package com.csdj.springboottest.config;

import org.apache.ibatis.datasource.pooled.PooledDataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

/**
 * mybatis 的配置类
 * author:cloud
 * date:2021/4/21 0021
 **/
//@Configuration
public class MybatisConfig {

    @Bean
    public DataSource createDatasource(){
        DataSource dataSource = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql:///shop","root","123456");
        return dataSource;
    }

    @Bean("sqlSessionFactoryBean")
    public SqlSessionFactoryBean createSfb(DataSource dataSource){
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        bean.setTypeAliasesPackage("com.csdj.springboottest.entity");
        return bean;
    }

    @Bean
    public MapperScannerConfigurer createMsc(SqlSessionFactoryBean factoryBean){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        msc.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
        msc.setBasePackage("com.csdj.springboottest.mapper");
        return msc;
    }
}
